package com.jy.modules.cims.sysassist;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.exception.CimsExceptionConstant;
import com.jy.modules.cims.common.exception.SysAssistException;
import com.jy.modules.cims.common.sysUtils.DateTimeUtils;
import com.jy.modules.cims.common.sysUtils.SpringContextUtils;
import com.jy.modules.cims.common.sysUtils.SimpleStringUtils;
import com.jy.modules.cims.component.sysAssist.CSysAssistImpl;
import com.jy.modules.cims.component.sysAssist.ICSysAssist;
import com.jy.modules.cims.data.interact.core.CoreStatusResponse;
import com.jy.modules.cims.interact.CCInteractImpl;

/**
 * 系统辅助服务
 */
@Service("com.jy.modules.cims.sysassist.SysAssistService")
public class SysAssistService {
	private static final Logger logger = LoggerFactory.getLogger(SysAssistService.class);

	@Autowired
	public SpringContextUtils springContextUtil;

	/**
	 * 根据业务类型生成业务单号 采用默认规则生成，后续有个性化差异的时候再调整 格式：zz20150101000001
	 * 
	 * @param type
	 *            业务类型
	 * @return
	 */
	@Transactional(propagation = Propagation.REQUIRES_NEW, timeout = 30)
	public String getBizNoForNewTran(String type) throws SysAssistException {
		ICSysAssist sysAssist = springContextUtil.getBean(CSysAssistImpl.class);

		return sysAssist.getBizNo(type);
	}

	/**
	 * @description 校验核心日终状态
	 * @return errMsg
	 */
	public String checkCoreStatus() {
		String errMsg = null;
		CCInteractImpl interactImpl = springContextUtil.getBean(CCInteractImpl.class);
		CoreStatusResponse coreStatusResponse = interactImpl.queryCoreStatus(DateTimeUtils.formatDate(new java.util.Date(),"yyyy-MM-dd"));
		if (null == coreStatusResponse) {
			errMsg = CimsExceptionConstant.CORE_EXCEPTION;
			logger.info(errMsg);
		} else {
			if ("0".equals(coreStatusResponse.getCoreEndStatus())) {
				errMsg = CimsExceptionConstant.CORE_END;
				logger.info(errMsg);
			}
		}

		return errMsg;
	}

	/**
	 * @description 校验核心日终状态
	 * @param recursion
	 *            是否递归检查 (true：是 ; false：否)
	 * @return errMsg
	 */
	public String checkCoreStatus(boolean recursion) {
		String errMsg = checkCoreStatus();
		if (SimpleStringUtils.hasText(errMsg) && recursion) {
			try {
				Thread.sleep(1000 * 60 * 10);// 暂停时间 10分钟：1000 * 60 * 10
			} catch (InterruptedException e) {
				logger.info(e.getMessage());
			}
			errMsg = checkCoreStatus(recursion);
		}

		return errMsg;
	}

	/**
	 * @description 校验更新债权价值
	 */
	public void checkUpdateCreditValue() {
		String doUpdateCreditValueJob = CimsConstant.doUpdateCreditValueJobMap.get(DateTimeUtils.formatDate(new java.util.Date(),
				DateTimeUtils.DEFAULT_DATE_FORMAT));
		if (SimpleStringUtils.hasText(doUpdateCreditValueJob)) {
			logger.info("当天的债权价值更新完毕。。。");
		} else {
			logger.info("当天的债权价值未更新或正在更新。。。");
			try {
				Thread.sleep(1000 * 60 * 10); // 暂停时间 10分钟：1000 * 60 * 10
			} catch (InterruptedException e) {
				logger.info(e.getMessage());
			}

			checkUpdateCreditValue();
		}
	}
	
	/**
	 * @description 校验还款计划和结算计划任务
	 */
	public void checkCreatPlan() {
		String doUpdateCreditValueJob = CimsConstant.doCreatPlanJobMap.get(DateTimeUtils.formatDate(new java.util.Date(),
				DateTimeUtils.DEFAULT_DATE_FORMAT));
		if (SimpleStringUtils.hasText(doUpdateCreditValueJob)) {
			logger.info("还款计划和结算计划任务完毕。。。");
		} else {
			logger.info("还款计划和结算计划任务未执行或正在执行。。。");
			try {
				Thread.sleep(1000 * 60 * 10); // 暂停时间 10分钟：1000 * 60 * 10
			} catch (InterruptedException e) {
				logger.info(e.getMessage());
			}

			checkCreatPlan();
		}
	}
}